package com.tembli.skills.web;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ReverseComparator;

import com.tembli.skills.service.ProjectService;

public class ProjectList implements Serializable {
	private ProjectService projectService;
	private String sortColumn = "id";
	private boolean ascending = true;

	public String getSortColumn() {
		return sortColumn;
	}

	public void setSortColumn(final String sortColumn) {
		this.sortColumn = sortColumn;
	}

	public boolean isAscending() {
		return ascending;
	}

	public void setAscending(final boolean ascending) {
		this.ascending = ascending;
	}

	public void setProjectService(final ProjectService projectService) {
		this.projectService = projectService;
	}

	public List getProjects() {
		List projects = projectService.getProjects();

		Comparator comparator = null;

		if (sortColumn.equalsIgnoreCase("name")) {
			comparator = new BeanDateComparator(sortColumn);
		} else {
			comparator = new BeanComparator(sortColumn);
		}

		if (!ascending) {
			comparator = new ReverseComparator(comparator);
		}

		Collections.sort(projects, comparator);

		return projects;
	}
}
